import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

public class test1408 {
    public List<String> stringMatching(String[] words) {
        List<String> res = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < words.length; i++) {
            sb.append(words[i]+",");
        }
        String s = sb.toString();
        for (int i = 0; i < words.length; i++) {
            if (sb.indexOf(words[i]) != sb.lastIndexOf(words[i])) {
                res.add(words[i]);
            }
        }
        return res;
    }

    @Test
    public void test(){
        String[] words = new String[]{"leetcoder", "leetcode","od","hamlet","am"};
        List<String> list = stringMatching(words);
        list.forEach(System.out::println);
    }
}
